package com.finger.crowd.mvc.controller;

import com.finger.crowd.entity.Role;
import com.finger.crowd.service.api.RoleService;
import com.finger.crowd.util.ResultEntity;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
public class RoleController {
    @Autowired
    private RoleService roleService;

    // 保存role
    @ResponseBody
    @RequestMapping("/role/to/save.json")
    public String saveRole(@RequestParam("roleName") String roleName,
                           HttpServletResponse resp) {
        int resultCounts = roleService.saveRole(roleName);
        if (resultCounts >= 1) {
            try {
                resp.getWriter().write("SUCCESS");
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            try {
                resp.getWriter().write("FAILED");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    // 分页
    @PreAuthorize("hasRole('部长')")
    @ResponseBody
    @RequestMapping("/role/page/to/info.json")
    public ResultEntity<PageInfo<Role>> getPageInfoByRole(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                          @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                          @RequestParam(value = "keyword", defaultValue = "") String keyword) {
        PageInfo<Role> pageInfo = roleService.getPageInfoForRole(pageNum, pageSize, keyword);
        ResultEntity<PageInfo<Role>> re = ResultEntity.successWithData(pageInfo);
        return re;
    }

    // 根据id查找角色
    @RequestMapping("/role/get/edit.json")
    @ResponseBody
    public ResultEntity<Role> getRoleById(@RequestParam("id") Integer id) {
        Role role = roleService.selectRoleById(id);
        if (role != null) {
            return ResultEntity.successWithData(role);
        }
        return ResultEntity.failed("请求失败");
    }

    // 更新角色
    @RequestMapping("/role/to/update.json")
    @ResponseBody
    public String updateRole(Role role) {
        int resultCounts = roleService.updateRole(role);
        if (resultCounts != 0) {
            return "SUCCESS";
        }
        return "FAILED";
    }

    @ResponseBody
    @RequestMapping("/role/remove/by/id.json")
    public ResultEntity removeRoleById(@RequestBody List<Integer> ids) {
        int resultCounts = roleService.remove(ids);
        if (resultCounts != 0) {
            return ResultEntity.successWithOutData();
        } else {
            return ResultEntity.failed("");
        }
    }

}
